/*
 * Copyright 2007-2008 Andrew O'Malley
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package ws.quokka.plugin.help;

import ws.quokka.core.bootstrap_util.Assert;
import ws.quokka.core.bootstrap_util.VoidExceptionHandler;
import ws.quokka.core.util.AnnotatedProperties;
import ws.quokka.core.util.URLs;
import ws.quokka.core.util.xml.Document;
import ws.quokka.core.util.xml.Element;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.net.URL;

import java.util.Iterator;
import java.util.Map;


/**
 *
 */
public class DepSetHelpGenerator extends AbstractHelpGenerator {
    //~ Methods --------------------------------------------------------------------------------------------------------

    protected URL getMetadataURL() {
        return URLs.toURL(artifact.getLocalCopy(), plugin.getDependencySetPath(artifact.getId()));
    }

    protected void addContent(Document out, Element outBodyEl) {
        outBodyEl.addChild("h2").addText("Dependency Set");
        outBodyEl.addChild("pre").addText(readURL(getMetadataURL()));

        URL propertiesUrl = URLs.toURL(artifact.getLocalCopy(),
                "META-INF/quokka/" + artifact.getId().toPathString() + "/quokka.properties");

        if (propertiesUrl != null) {
            AnnotatedProperties properties = new AnnotatedProperties();
            properties.load(propertiesUrl);

            if (properties.size() != 0) {
                outBodyEl.addChild("h2").addText("Properties");

                Element tableEl = outBodyEl.addChild("table");

                for (Iterator i = properties.entrySet().iterator(); i.hasNext();) {
                    Map.Entry entry = (Map.Entry)i.next();
                    Element trEl = tableEl.addChild("tr");
                    trEl.addChild("td").addText(entry.getKey().toString());
                    trEl.addChild("td").addText(entry.getValue().toString());
                }
            }
        }
    }

    private String readURL(final URL url) {
        final StringBuffer sb = new StringBuffer();
        new VoidExceptionHandler() {
                public void run() throws Exception {
                    InputStream inputStream = url.openStream();
                    Assert.isTrue(inputStream != null, "url does not exist: " + url.toString());

                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

                    try {
                        while (true) {
                            String line = reader.readLine();

                            if (line == null) {
                                break;
                            }

                            sb.append(line).append("\n");
                        }
                    } finally {
                        reader.close();
                    }
                }
            };

        return sb.toString();
    }
}
